from browser . logic import browser_logic

class mediator_fake :
    def __init__ ( self ) :
        self . _added_table_rows = [ ]
        self . _added_table_rows_before_table_creation = [ ]
        self . _browser_number = None
        self . _browser_existing_name = None
        self . _browser_creation_time = None
        self . _browser_modification_time = None
        self . _browser_size = None
        self . _knowledge_bases_fetched = None
        self . _knowledge_base_receiving_callback = None
        self . _all_knowledge_bases = [ ]
        self . _selection_history = [ ]
        self . _entry_text = None
        self . _entry_focused = False
        self . _bold_table_rows = [ ]
        self . _gui_call_history = [ ]
        self . _action_button_added = False
    def add_title_label_to_browser ( self , title_text ) :
        self . _gui_call_history += [ [ mediator_fake . add_title_label_to_browser , title_text ] ]
    def add_table_to_browser ( self ) :
        self . _gui_call_history += [ [ mediator_fake . add_table_to_browser ] ]
        self . _added_table_rows_before_table_creation = list ( self . _added_table_rows )
    def add_return_button_to_browser ( self ) :
        self . _gui_call_history += [ [ mediator_fake . add_return_button_to_browser ] ]
    def add_action_button_to_browser ( self , action_text ) :
        self . _action_button_added = True
        self . _gui_call_history += [ [ mediator_fake . add_action_button_to_browser , action_text ] ]
    def add_title_row_to_browser ( self , number , name , creation_time , modification_time , size ) :
        self . _added_table_rows += [ [ mediator_fake . add_title_row_to_browser , number , name , creation_time , modification_time , size ] ]
    def add_existing_row_to_browser ( self , number , name , creation_time , modification_time , size ) :
        self . _added_table_rows += [ [ mediator_fake . add_existing_row_to_browser , number , name , creation_time , modification_time , size ] ]
    def add_empty_row_to_browser ( self ) :
        self . _added_table_rows += [ [ mediator_fake . add_empty_row_to_browser ] ]
    def add_action_row_to_browser ( self ) :
        self . _added_table_rows += [ [ mediator_fake . add_action_row_to_browser ] ]
    def set_entry_text_in_browser ( self , text ) :
        self . _entry_text = text
    def focus_entry_in_browser ( self ) :
        self . _entry_focused = True
    def select_table_row ( self , row ) :
        self . _selection_history += [ [ mediator_fake . select_table_row , row ] ]
    def localization_browser_number ( self ) :
        return self . _browser_number
    def localization_browser_existing_name ( self ) :
        return self . _browser_existing_name
    def localization_browser_creation_time ( self ) :
        return self . _browser_creation_time
    def localization_browser_modification_time ( self ) :
        return self . _browser_modification_time
    def localization_browser_size ( self ) :
        return self . _browser_size
    def fetch_all_knowledge_bases ( self ) :
        self . _knowledge_bases_fetched = True
        for kb in self . _all_knowledge_bases :
            self . _knowledge_base_receiving_callback ( * kb )
    def bold_table_row ( self , row ) :
        self . _bold_table_rows += [ row ]
    
class browser_logic_show_browser_tests :
    'browser_logic_show_browser'
    def __init__ ( self , test ) :
        self . _test = test
        self . _mediator = mediator_fake ( )
        self . _browser_logic = browser_logic ( self . _mediator )
    def test_show_browser_existing_menu_title ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = [ [ None , None , None , None ] ]
        self . _mediator . _browser_number = 'my number'
        self . _mediator . _browser_existing_name = 'my name'
        self . _mediator . _browser_creation_time = 'my creation time'
        self . _mediator . _browser_modification_time = 'my modification time'
        self . _mediator . _browser_size = 'my size'
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _added_table_rows [ 0 : 1 ] ,
            [ [ mediator_fake . add_title_row_to_browser
            , 'my number'
            , 'my name'
            , 'my creation time'
            , 'my modification time'
            , 'my size'
            ] ] )
    def test_show_browser_fetch_all_knowledge_bases ( self ) :
        self . _browser_logic . show_browser ( )
        self . _test . assume_true ( self . _mediator . _knowledge_bases_fetched )
    def test_show_browser_existing_menu_items ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'name1' , 'creation time1' , 'modification time1' , 'size1' ]
            , [ 'name3' , 'creation time2' , 'modification time2' , 'size2' ]
            , [ 'name2' , 'creation time3' , 'modification time3' , 'size3' ]
            ]
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _added_table_rows [ 1 : 4 ] ,
            [ [ mediator_fake . add_existing_row_to_browser , '01' , 'name1' , 'creation time1' , 'modification time1' , 'size1' ]
            , [ mediator_fake . add_existing_row_to_browser , '02' , 'name2' , 'creation time3' , 'modification time3' , 'size3' ]
            , [ mediator_fake . add_existing_row_to_browser , '03' , 'name3' , 'creation time2' , 'modification time2' , 'size2' ]
            ] )
    def test_show_browser_clear_old_knowledge_bases ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = [ [ 'name1' , 'creation time1' , 'modification time1' , 'size1' ] ]
        self . _browser_logic . show_browser ( )
        self . _mediator . _added_table_rows = [ ]
        self . _mediator . _all_knowledge_bases = [ [ 'name2' , 'creation time2' , 'modification time2' , 'size2' ] ]
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _added_table_rows [ 1 : 2 ] ,
            [ [ mediator_fake . add_existing_row_to_browser , '01' , 'name2' , 'creation time2' , 'modification time2' , 'size2' ] ]
            )
    def test_show_browser_action_part ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = [ [ None , None , None , None ] ]
        self . _mediator . _browser_number = 'my number'
        self . _mediator . _browser_creation_time = 'my creation time'
        self . _mediator . _browser_modification_time = 'my modification time'
        self . _mediator . _browser_size = 'my size'
        self . _browser_logic . configure_action_title_name_text_for_browser ( 'my name' )
        self . _browser_logic . configure_remove_action_part_when_no_entries_for_browser ( )
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _added_table_rows [ 2 : ] ,
            [ [ mediator_fake . add_empty_row_to_browser
              ]
            , [ mediator_fake . add_title_row_to_browser
              , 'my number'
              , 'my name'
              , 'my creation time'
              , 'my modification time'
              , 'my size'
              ]
            , [ mediator_fake . add_action_row_to_browser
              ] 
            ] )
    def test_show_browser_action_part_when_no_entries ( self ) :
        self . _mediator . _browser_number = 'my number'
        self . _mediator . _browser_creation_time = 'my creation time'
        self . _mediator . _browser_modification_time = 'my modification time'
        self . _mediator . _browser_size = 'my size'
        self . _browser_logic . configure_remove_action_part_when_no_entries_for_browser ( )
        self . _browser_logic . configure_keep_action_part_when_no_entries_for_browser ( )
        self . _browser_logic . configure_action_title_name_text_for_browser ( 'my name' )
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _added_table_rows ,
            [ [ mediator_fake . add_title_row_to_browser
              , 'my number'
              , 'my name'
              , 'my creation time'
              , 'my modification time'
              , 'my size'
              ]
            , [ mediator_fake . add_action_row_to_browser
              ] 
            ] )
    def test_show_browser_configured_to_remove_action_part_when_no_entries ( self ) :
        self . _browser_logic . configure_remove_action_part_when_no_entries_for_browser ( )
        self . _browser_logic . show_browser ( )
        self . _test . assume_empty ( self . _mediator . _added_table_rows )
    def test_show_browser_select_single_row ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = [ [ None , None , None , None ] ]
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _selection_history ,
            [ [ mediator_fake . select_table_row , 1 ]
            ] )
    def test_show_browser_dont_select_when_no_existing_knowledge_bases ( self ) :
        self . _browser_logic . show_browser ( )
        self . _test . assume_empty ( self . _mediator . _selection_history )
    def test_show_browser_select_entry_with_greater_modification_time ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'name1' , None , 'time1' , None ]
            , [ 'name2' , None , 'time3' , None ]
            , [ 'name3' , None , 'time2' , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _selection_history ,
            [ [ mediator_fake . select_table_row , 2 ]
            ] )
    def test_show_browser_active_entry_when_single_entry ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = [ [ 'my name' , None , None , None ] ]
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _entry_text , 'my name' )
    def test_show_browser_active_entry_when_several_entries ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = \
            [ [ 'name1' , None , 'time1' , None ]
            , [ 'name2' , None , 'time3' , None ]
            , [ 'name3' , None , 'time4' , None ]
            , [ 'name4' , None , 'time2' , None ]
            ]
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _entry_text , 'name3' )
        self . _test . assume_equal ( self . _mediator . _bold_table_rows , [ 3 ] )
    def test_show_browser_focus_entry ( self ) :
        self . _browser_logic . show_browser ( )
        self . _test . assume_true ( self . _mediator . _entry_focused )
    def test_show_browser_focus_entry_when_configured_to_remove_action_part_and_no_entries ( self ) :
        self . _browser_logic . configure_remove_action_part_when_no_entries_for_browser ( )
        self . _browser_logic . show_browser ( )
        self . _test . assume_false ( self . _mediator . _entry_focused )
    def test_show_browser_focus_entry_when_configured_to_remove_action_part_and_there_are_entries ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = [ [ None , None , None , None ] ]
        self . _browser_logic . configure_remove_action_part_when_no_entries_for_browser ( )
        self . _browser_logic . show_browser ( )
        self . _test . assume_true ( self . _mediator . _entry_focused )
    def test_show_browser_add_action_button_to_browser ( self ) :
        self . _browser_logic . configure_action_text_for_browser ( 'my action' )
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _gui_call_history [ - 1 : ] ,
            [ [ mediator_fake . add_action_button_to_browser , 'my action' ] 
            ] )
    def test_show_browser_add_action_button_to_browser_when_configured_to_remove_action_part_and_no_entries ( self ) :
        self . _browser_logic . configure_remove_action_part_when_no_entries_for_browser ( )
        self . _browser_logic . show_browser ( )
        self . _test . assume_false ( self . _mediator . _action_button_added )
    def test_show_browser_add_action_button_to_browser_when_configured_to_remove_action_part_and_there_are_entries ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = [ [ None , None , None , None ] ]
        self . _browser_logic . configure_remove_action_part_when_no_entries_for_browser ( )
        self . _browser_logic . show_browser ( )
        self . _test . assume_true ( self . _mediator . _action_button_added )
    def test_show_browser_add_return_button_to_browser ( self ) :
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _gui_call_history [ - 2 : - 1 ] ,
            [ [ mediator_fake . add_return_button_to_browser ] 
            ] )
    def test_show_browser_add_table_to_browser ( self ) :
        self . _browser_logic . show_browser ( )
        self . _test . assume_empty ( self . _mediator . _added_table_rows_before_table_creation )
        self . _test . assume_equal ( self . _mediator . _gui_call_history [ - 3 : - 2 ] ,
            [ [ mediator_fake . add_table_to_browser ] 
            ] )
    def test_show_browser_add_title_label_to_browser ( self ) :
        self . _mediator . _knowledge_base_receiving_callback = self . _browser_logic . receive_knowledge_base
        self . _mediator . _all_knowledge_bases = [ [ None , None , None , None ] ]
        self . _browser_logic . configure_title_text_for_browser ( 'my title' )
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _gui_call_history [ : 1 ] ,
            [ [ mediator_fake . add_title_label_to_browser , 'my title' ] 
            ] )
    def test_show_browser_add_title_label_to_browser_when_no_entries ( self ) :
        self . _browser_logic . configure_title_text_when_no_entries_for_browser ( 'my title' )
        self . _browser_logic . show_browser ( )
        self . _test . assume_equal ( self . _mediator . _gui_call_history [ : 1 ] ,
            [ [ mediator_fake . add_title_label_to_browser , 'my title' ] 
            ] )
